package com.byx.chatclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.byx.chatclient.R;
import com.byx.chatclient.presenter.BasePresenter;
import com.byx.chatclient.view.IBaseView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper;
import okhttp3.OkHttpClient;

/**
 * BaseActivity
 *
 * @author Superb
 * @date 2021/2/22 17:39
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView,BGASwipeBackHelper.Delegate {
    protected static final String TAG = BaseActivity.class.getSimpleName();
    protected BGASwipeBackHelper mSwipeBackHelper;

    protected P presenter;

    private Unbinder unbinder;

//    protected BaseActivity<T> activity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
        // 在 super.onCreate(savedInstanceState) 之前调用该方法
        initSwipeBackFinish();
        super.onCreate(savedInstanceState);
//        activity = this;
        setRootView(savedInstanceState);
        unbinder = ButterKnife.bind(this);
        presenter = createPresenter();
        initialize();
        setListeners();
    }

    protected abstract P createPresenter();

    private void initSwipeBackFinish() {
        mSwipeBackHelper = new BGASwipeBackHelper(this, this);

        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
        // 下面几项可以不配置，这里只是为了讲述接口用法。

        // 设置滑动返回是否可用。默认值为 true
        mSwipeBackHelper.setSwipeBackEnable(true);
        // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
        mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true);
        // 设置是否是微信滑动返回样式。默认值为 true
        mSwipeBackHelper.setIsWeChatStyle(true);
        // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
        mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow);
        // 设置是否显示滑动返回的阴影效果。默认值为 true
        mSwipeBackHelper.setIsNeedShowShadow(true);
        // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
        mSwipeBackHelper.setIsShadowAlphaGradient(true);
        // 设置触发释放后自动滑动返回的阈值，默认值为 0.3f
        mSwipeBackHelper.setSwipeBackThreshold(0.3f);
        // 设置底部导航条是否悬浮在内容上，默认值为 false
        mSwipeBackHelper.setIsNavigationBarOverlap(false);
    }

    /**
     * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回，如果某个界面不想支持滑动返回则重写该方法返回 false 即可
     *
     * @return
     */
    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    /**
     * 正在滑动返回
     *
     * @param slideOffset 从 0 到 1
     */
    @Override
    public void onSwipeBackLayoutSlide(float slideOffset) {
    }

    /**
     * 没达到滑动返回的阈值，取消滑动返回动作，回到默认状态
     */
    @Override
    public void onSwipeBackLayoutCancel() {
    }

    /**
     * 滑动返回执行完毕，销毁当前 Activity
     */
    @Override
    public void onSwipeBackLayoutExecuted() {
        mSwipeBackHelper.swipeBackward();
    }

    @Override
    public void onBackPressed() {
        // 正在滑动返回的时候取消返回按钮事件
        if (mSwipeBackHelper.isSliding()) {
            return;
        }
        mSwipeBackHelper.backward();
    }

    protected abstract void setRootView(Bundle savedInstanceState);

    protected abstract void initialize();

    protected abstract void setListeners();

    protected void start(){

    }

    protected void restart(){

    }

    protected void resume(){

    }

    protected void pause(){

    }

    protected void stop(){

    }

    protected void destroy(){

    }

    @Override
    protected void onStart() {
        super.onStart();
        if(presenter != null){
            presenter.attachView(this);
        }
        start();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        restart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stop();
    }

    @Override
    protected void onDestroy() {
        destroy();
        if(presenter != null){
            presenter.detachView();
        }
        if(unbinder != null){
            unbinder.unbind();
            unbinder = null;
        }
        super.onDestroy();
    }

    protected void startActivity(Class<?> cls){
        startActivity(new Intent(this,cls));
    }

    protected void startActivity(Class<?> cls, Map<String,Object> params){
        Intent intent = new Intent(this,cls);
        if(null != params){
            for (String key : params.keySet()){
                putExtraToIntent(intent,key,params.get(key));
            }
        }
        startActivity(intent);
    }

    public static void putExtraToIntent(Intent intent, String key, Object val) {
        if (val instanceof Boolean) {
            intent.putExtra(key, (Boolean) val);
        } else if (val instanceof Boolean[]) {
            intent.putExtra(key, (Boolean[]) val);
        } else if (val instanceof String) {
            intent.putExtra(key, (String) val);
        } else if (val instanceof String[]) {
            intent.putExtra(key, (String[]) val);
        } else if (val instanceof Integer) {
            intent.putExtra(key, (Integer) val);
        } else if (val instanceof Integer[]) {
            intent.putExtra(key, (Integer[]) val);
        } else if (val instanceof Long) {
            intent.putExtra(key, (Long) val);
        } else if (val instanceof Long[]) {
            intent.putExtra(key, (Long[]) val);
        } else if (val instanceof Double) {
            intent.putExtra(key, (Double) val);
        } else if (val instanceof Double[]) {
            intent.putExtra(key, (Double[]) val);
        } else if (val instanceof Float) {
            intent.putExtra(key, (Float) val);
        } else if (val instanceof Float[]) {
            intent.putExtra(key, (Float[]) val);
        } else if (val instanceof Serializable) {
            intent.putExtra(key, (Serializable) val);
        }
    }

    @Override
    public void startActivity(Intent intent) {
//        super.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
        super.startActivity(intent);
    }

    @Override
    public void toastQuick(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void toastSlow(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
    }
}
